package ejb;

import javax.ejb.EJB;
import javax.ejb.Stateless;
import javax.ejb.LocalBean;
import hibernate.ProductDAO;
import hibernate.ProductCategory;
import hibernate.Product;
import hibernate.User;
import java.util.ArrayList;
import java.util.List;
import hibernate.UserRole;
import hibernate.Request;
import hibernate.RequestDAO;

/**
 *
 * @apodoba
 */
@Stateless
@LocalBean
public class ProductManager extends ManagedEJB {

    @EJB
    private ProductDAO productDAO;
    
    @EJB
    private Security security;
    private UserRole roleManager = UserRole.GET_MANAGER_ROLE();
    private UserRole rolePresident = UserRole.GET_PRESIDENT_ROLE();
    
    @EJB
    private RequestDAO requestDAO;

    @Override
    public boolean init(ServerManager serverManager) {
        if (!super.init(serverManager)) {
            return false;
        }
        security = serverManager.getSecurity();
        return true;
    }

    // authorize user return true
    private boolean isAuthenticatedUser() {
        return security.isAuthorized();
    }

    // role manager, president return true 
    private boolean isManager(User currentUser) {
        if (currentUser.getRole().equals(roleManager) || currentUser.getRole().equals(rolePresident)) {
            return true;
        }
        return false;
    }

    // return list of all products
    public List<Product> getAllProducts() {
        if (isAuthenticatedUser()) {
            List<Product> allProducts = productDAO.getAllProducts();

            if (allProducts != null && !allProducts.isEmpty()) {
                return allProducts;
            }
        }
        return null;
    }

    // return list of products in one category by id of category
    public List<Product> getAllProductsInCategory(int idProductCategory) {
        if (isAuthenticatedUser()) {
            List<Product> allProductsInCategories = productDAO.getAllProductsInCategory(idProductCategory);

            if (allProductsInCategories != null && !allProductsInCategories.isEmpty()) {
                return allProductsInCategories;
            }
        }
        return null;
    }

    // get one product 
    public Product getProductByID(int idProduct) {
        if (isAuthenticatedUser()) {
            Product product = productDAO.getProductByID(idProduct);
            return product;
        }
        return null;
    }

    // return true if product saved
    public boolean saveNewProduct(String nameNewProduct, ProductCategory productCategory, boolean common) {
        if (isManager(security.getCurrentUser())) {

            Product newProduct = productDAO.createEmptyProduct();
            newProduct.setDescription(nameNewProduct);
            newProduct.setProdCategory(productCategory);
            newProduct.setCommon(common);

            List<Product> allProducts = productDAO.getAllProducts();
            List<String> descriptionAllProducts = new ArrayList<String>();

            for (Product iterAllProduct : allProducts) {
                descriptionAllProducts.add(iterAllProduct.getDescription());

                if (descriptionAllProducts.contains(newProduct.getDescription())) {
                    return false;
                }
            }
            productDAO.addAndSaveNewProduct(newProduct);
            return true;
        }

        return false;
    }

    public boolean deleteProduct(Product deleteProduct) {
        if (isAuthenticatedUser() && isManager(security.getCurrentUser())) {
            List<Request> allRequest = requestDAO.getAllRequests();

            if (allRequest != null && !allRequest.isEmpty()) {
                for (Request iterRequest : allRequest) {
                    if (iterRequest.getProduct().equals(deleteProduct)) {
                        return false;
                    }
                }
            }
            productDAO.deleteProduct(deleteProduct);
            return true;
        }
        return false;
    }

    // return true if product update
    public boolean updateProduct(Product updateProduct) {
        if (isManager(security.getCurrentUser())) {
            List<ProductCategory> allCategories = productDAO.getAllProductCategories();
            List<Product> allProducts = productDAO.getAllProducts();

            if (allProducts != null && !allProducts.isEmpty() && allCategories.contains(updateProduct.getProdCategory())) {
                String descriptionProduct;
                for (Product iterAllProducts : allProducts) {
                    descriptionProduct = iterAllProducts.getDescription();

                    if (descriptionProduct.equals(updateProduct.getDescription()) && iterAllProducts.getId() != updateProduct.getId()) {
                        return false;
                    }


                }
            }
            productDAO.updateProduct(updateProduct);
            return true;
        }
        return false;
    }
    // get all categories of product

    public List<ProductCategory> getAllCategories() {
        if (isAuthenticatedUser()) {
            List<ProductCategory> allCategories = productDAO.getAllProductCategories();

            if (allCategories != null && !allCategories.isEmpty()) {
                return allCategories;
            }
        }
        return null;
    }

    // get category by id of category
    public ProductCategory getCategoryByID(int idCategory) {
        if (isAuthenticatedUser()) {
            ProductCategory category = productDAO.getProductCategoryByID(idCategory);
            return category;
        }
        return null;
    }

    // return true if category saved
    public boolean saveNewCategory(String nameNewCategory) {

        if (isManager(security.getCurrentUser())) {

            ProductCategory newCategory = productDAO.createEmptyProductCategory();
            newCategory.setDescription(nameNewCategory);

            List<ProductCategory> allCategories = productDAO.getAllProductCategories();

            if (allCategories != null && !allCategories.isEmpty()) {

                for (ProductCategory iterAllCategory : allCategories) {
                    if (iterAllCategory.equals(newCategory)) {
                        return false;
                    }
                }
            }
            productDAO.addAndSaveNewProductCategory(newCategory);
            return true;
        }
        return false;
    }

    public void deleteCategory(ProductCategory deleteCategory) {
        if (isManager(security.getCurrentUser())) {
            productDAO.deleteProductCategory(deleteCategory);
        }
    }

    // return true if category update
    public boolean updateCategory(ProductCategory updateCategory) {
        if (isManager(security.getCurrentUser())) {
            List<ProductCategory> allCategories = productDAO.getAllProductCategories();

            if (allCategories != null && !allCategories.isEmpty()) {
                for (ProductCategory iterAllProduct : allCategories) {
                    if (iterAllProduct.equals(updateCategory)) {
                        return false;
                    }
                }
            }
            productDAO.updateProductCategory(updateCategory);
            return true;
        }
        return false;
    }
}
